home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Simple question about constructors
- Date: Mon, 04 Mar 1996 22:00:20 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.00000055.007de71c@fred>
- References: <4hbadp$t0p@cloner4.netcom.com>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: client77.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <4hbadp$t0p@cloner4.netcom.com>, stefmit@ix.netcom.com wrote:
- >
- > It seems that the "copy constructor" day is one of my worst in the C++
- > learning process. Now I ran into another problem:
- > I have a linked list based on a template <class My_type>, which has a node
- > class with (obviously) a pointer (let's call it *next) and an element field of
- > the type My_type (called data). Then, I have a list class using the node
- > above, with its own pointer *head (I am trying to build a sort of stack). I
- > was able to build the stack (this was pretty easy), but then I tried to define
- > a copy constructor in the list, using something similar to:
- > template <class My_type>
- > List<My_type> :: List(const List &newList)
- > { head -> data = newList.head -> data;
- > head -> next = newList.head -> next; }
- > I am almost positive that the second line crashes my program when I try to
- > intialize an object list based on an existing one (as I copy pointers, instead
- > of what they point to - i.e. next), but I have no clue on how to address the
- > problem. The example I have in my book deals with strings, thus the
- > availability of strcpy or strup functions, that handle the copy. Would anybody
- > care to enlighten me on this?
- > TIA.
-
- I think you want to copy every element of the old list into the new one:
-
- template <class My_type>
- class List
- {
- class node // definition of node
- {
- public:
- My_type data;
- node *next;
- node(const My_type &d)
- : data(d), // data is initialized using its copy constructor
- next(0) // I like to initialized pointer to useful data
- {} // nothing else to do
- };
-
- node *head;
-
- public:
- List() : head(0) {} // create an empty list
- List(const List<My_type> &); // copy constructor
- // I suppose other member functions will be needed
- };
-
- template <class My_type>
- List<My_type>::List(const List<My_type> &oldList)
- : head(0)
- {
- if (oldList.head)
- {
- head = new node(oldList.head->data); // create new node with copy of
- // old head data. That will be the
- // new head
- node *nOld = oldList.head->next;
- node *nNew = head;
- while (nOld)
- {
- nNew->next = new node(n->data) // copy the remaining nodes
- nNew = nNew->next;
- nOld = nOld->next
- }
- }
- }
-
-
- Note: the node class relies on a My_type copy constructor to copy the My_type
- data.
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-